package com.dragon.modules.yuegou.service.impl.merchant;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.exception.DragonException;
import com.dragon.modules.yuegou.entity.merchant.MerchantDeliveryEntity;
import com.dragon.modules.yuegou.entity.product.SpuEntity;
import com.dragon.modules.yuegou.enums.code.MerchantCode;
import com.dragon.modules.yuegou.enums.merchant.MerchantDeliveryType;
import com.dragon.modules.yuegou.enums.product.SpuPublishStatus;
import com.dragon.modules.yuegou.enums.product.SpuShowModule;
import com.dragon.modules.yuegou.mapper.merchant.MerchantDeliveryMapper;
import com.dragon.modules.yuegou.model.merchant.MerchantDelivery;
import com.dragon.modules.yuegou.query.merchant.MerchantDeliveryQuery;
import com.dragon.modules.yuegou.query.product.SpuQuery;
import com.dragon.modules.yuegou.service.merchant.MerchantDeliveryService;
import com.dragon.modules.yuegou.service.product.SpuService;
import com.dragon.modules.yuegou.web.Check;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 商户配送信息 服务实现
 *
 * @author pbuilder generated
 * @date 2020-03-24 11:46:18
 * @version 2.0
 */
@Service("merMerchantDeliveryService")
public class MerchantDeliveryServiceImpl
		extends ServiceImpl<MerchantDeliveryMapper, MerchantDeliveryEntity>
		implements MerchantDeliveryService {

	@Autowired
	private SpuService spuService;

	@Override
	public MerchantDelivery configuration(String merchantId) {
		Check.notEmpty(merchantId, "商户ID不能为空");
		MerchantDeliveryEntity delivery = getById(merchantId);
		if (delivery == null) {
			// 不存在创建
			delivery = new MerchantDelivery();
			delivery.setMerchantId(merchantId);
			delivery.setCreatedTime(LocalDateTime.now());
			save(delivery);
		}
		return new MerchantDelivery(delivery);
	}

	@Override
	public void modifyType(String merchantId, Integer type) {
		Check.notEmpty(merchantId, "商户ID不能为空");
		Check.notNull(type, "配送方式不能为空");

		// 查询配送信息
		MerchantDeliveryQuery query = new MerchantDeliveryQuery();
		query.setMerchantId(merchantId);
		MerchantDeliveryEntity delivery = getOne(query.toQueryWrapper());
		if (0== type){
			delivery.setType(null);
			// 关闭配送 判读改商户是否是上架的配送商品
			SpuQuery spuQuery = new SpuQuery();
			spuQuery.setMerchantId(delivery.getMerchantId());
			spuQuery.setPublishStatus(SpuPublishStatus.UPPER);
			spuQuery.setShowModule(SpuShowModule.GOHOME);
			List<SpuEntity> list = spuService.list(spuQuery.toQueryWrapper());
			if (CollectionUtils.isNotEmpty(list)) {
				throw new DragonException(MerchantCode.REMOVE_BEFORE_CLOSING);
			}
			baseMapper.updateType(merchantId);
		}else {
			delivery.setType(MerchantDeliveryType.AUTONOMY);
			// 修改商户配送信息
			updateById(delivery);
		}
	}

	@Override
	public BigDecimal balance(String merchantId) {
		return BigDecimal.ZERO;
	}

	@Override
	public MerchantDelivery createShop(String merchantId, String city, String area, Integer business, String email) {
		// 获得商户运费配置
		return configuration(merchantId);
	}


	@Override
	public String recharge(String merchantId, BigDecimal amount) {
		return null;
	}

}